# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=wxPython
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.0.2.0
_editraver=0.7.20
pkgrel=10
pkgdesc="A wxWidgets GUI toolkit for Python (mingw-w64)"
arch=('any')
license=("custom:wxWindows")
url="https://www.wxpython.org/"
depends=("${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('strip' 'staticlibs' 'buildflags')
source=(https://downloads.sourceforge.net/wxpython/${_realname}-src-${pkgver}.tar.bz2
        #https://editra.googlecode.com/files/Editra-${_editraver}.tar.gz
        001-mingw-python.patch
        002-system-includes.patch
        003-fix-cast-error.patch
        004-plot-fix.patch)
sha256sums=('d54129e5fbea4fb8091c87b2980760b72c22a386cb3b9dd2eebc928ef5e8df61'
            '61ca97f814b85b1274e751c694f9e3adc31ce1e22c56c9ca9c59e0337a36fef5'
            'bea571dfdade2c7b15e1feaf260834d571f49c36101a420104b933c3597f7ebf'
            'd7c4de5594149d2bba4a0ceb95d6a6a0c3c0e43ab9e876dce22440ccaad3b5f0'
            '6280e51dc8be1187b2b5a3cc35e08721bf9c2b5e3dacc8d08bc25bd0bdc3aea1')

prepare() {
  cd "${srcdir}/${_realname}-src-${pkgver}"
  patch -p1 -i ${srcdir}/001-mingw-python.patch
  patch -p1 -i ${srcdir}/002-system-includes.patch
  patch -p1 -i ${srcdir}/003-fix-cast-error.patch
  patch -p1 -i ${srcdir}/004-plot-fix.patch

  #cd ${srcdir}
  find . -type f -exec sed -i 's/env python/env python2/' {} \;
  #sed -i 's/sys.exit(1)//' Editra-${_editraver}/setup.py
}

build() {
  #[[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  #mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}
  export PYTHON=${MINGW_PREFIX}/bin/python2
  cd ${_realname}-src-${pkgver}
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --with-msw \
    --disable-mslu \
    --enable-shared \
    --enable-iniconf \
    --enable-iff \
    --enable-permissive \
    --disable-monolithic \
    --disable-mediactrl \
    --enable-unicode \
    --enable-accessibility \
    --disable-precomp-headers

  cd wxPython
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-headers=;-install-data=" \
  ${MINGW_PREFIX}/bin/python2 setup.py WXPORT=msw BUILD_ACTIVEX=0 UNICODE=1 COMPILER=mingw32 build
  #make VERBOSE=1
}

package() {
  cd "${srcdir}/wxPython-src-${pkgver}/wxPython"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-headers=;-install-data=" \
  ${MINGW_PREFIX}/bin/python2 setup.py NO_HEADERS=0 WXPORT=msw BUILD_ACTIVEX=0 UNICODE=1 COMPILER=mingw32 \
    install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  install -D -m644 ../docs/licence.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  local _dir=$(cygpath -am ${MINGW_PREFIX})
  for _f in ${pkgdir}${MINGW_PREFIX}/bin/*; do
    sed -e "s|${_dir}|${MINGW_PREFIX}|g" -i ${_f}
  done

  #cd "${srcdir}/Editra-${_editraver}"
  #MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  #${MINGW_PREFIX}/bin/python2 setup.py install --root="${pkgdir}" --prefix=${MINGW_PREFIX}
  #rm -r "${pkgdir}${MINGW_PREFIX}/lib/python2.7/site-packages/wx-3.0-gtk2/wx/tools/Editra"
}
